package com.demo.web.base;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.demo.entity.base.Page;

public class BaseResult<T> implements Serializable {
	private static final long serialVersionUID = -8550086827043094844L;
	private int currentPage; // 当前页
	private int pageSize = 20; // 每页显示的条数
	private int allCounts; // 总条数
	private int allPages; // 总页数
	private List<Integer> pageNum;
	private List<T> resultList; // 记录集合
	private Map<String, Object> param; // 记录着额外参数
	private static int num = 6;

	public BaseResult(Page page, int allCounts) {
		this.currentPage = page.getCurrentPage();
		this.allCounts = allCounts;
		pageNum = new ArrayList<Integer>();
		splitPage(page.getCurrentPage(), allCounts, page.getPageSize());
		System.out.println("当前页:" + currentPage + "页，共:" + allCounts + "条,一页：" + pageSize + "条");
	}

	public BaseResult(int currentPage, int allCounts, int pageSize) {
		this.currentPage = currentPage <= 0 ? 1 : currentPage;
		this.allCounts = allCounts;
		pageNum = new ArrayList<Integer>();
		splitPage(currentPage, allCounts, pageSize);
		System.out.println(pageNum.toString());
	}

	public int current2beginPage(int currentPage) {
		return (currentPage - 1) * pageSize;
	}

	/**
	 * 分页实例
	 * 
	 * @param currentPage
	 *            //当前页[>1]
	 * @param allCounts
	 *            //总条数[>1]
	 * @param pageNo
	 *            //一页条数[>1]
	 */
	public void splitPage(int cp, int ac, int pn) {
		if (num < 2)
			num = 8;
		int no = num % 2 == 0 ? num / 2 : num / 2 + 1;
		int ap = ac % pn == 0 ? ac / pn : ac / pn + 1;
		// ========begin===============
		if (cp > no + 1)
			pageNum.add(1);
		if (cp > no + 2)
			pageNum.add(0);
		int x = -no;
		if (ap - cp - no < 0) {
			x += ap - cp - no;
		}
		for (int i = -no; i <= no; i++) {
			int a = cp + x;
			x++;
			if (a < 1) {
				i--;
				continue;
			}
			if (a > ap)
				continue;
			if (x == 1) {
				pageNum.add(a);
			}// 当前页
			else
				pageNum.add(a);
		}
		if (cp < (ap - no) && (ap-pageNum.get(pageNum.size()-1))>1)
			pageNum.add(0);
		if (cp < (ap - no + 1) && (ap-pageNum.get(pageNum.size()-1))>0 )
			pageNum.add(ap);
		// ========end===============
		allPages = pageNum.size();
	}

	public static void main(String[] args) {
		//0表示...
		BaseResult<?> br=new BaseResult<>(17, 440, 20);
	}
	
	
	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getAllCounts() {
		return allCounts;
	}

	public void setAllCounts(int allCounts) {
		this.allCounts = allCounts;
	}

	public int getAllPages() {
		return allPages;
	}

	public void setAllPages(int allPages) {
		this.allPages = allPages;
	}

	public List<T> getResultList() {
		return resultList;
	}

	public void setResultList(List<T> resultList) {
		this.resultList = resultList;
	}

	public Map<String, Object> getParam() {
		return param;
	}

	public void setParam(Map<String, Object> param) {
		this.param = param;
	}

	public List<Integer> getPageNum() {
		return pageNum;
	}

	public void setPageNum(List<Integer> pageNum) {
		this.pageNum = pageNum;
	}
}
